﻿using System;
using System.Windows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Telerik.Windows.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCE.UI.PopWindows
{
    public partial class WorkPlan : EFViewCmes
    {
        public WorkPlan(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    fdStatus.IsEnabled = false;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (NotNullOrEmpty(this.fdBeganDate) && NotNullOrEmpty(this.fdEndDate))
            {
                string begin = this.fdBeganDate.GetDataValue().ToString();
                string end = this.fdEndDate.GetDataValue().ToString();
                if (begin.CompareTo(end) > 0)
                {
                    this.OpenEFWindowAlert("计划开始日期不能大于计划结束日期！", true);
                    return;
                }
            }
            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 弹出的新增、修改页面点击"确定"后的回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                string str = inInfo.GetMsg();
                string mark = inInfo.Get("mark").ToString();
                if ("may".Equals(mark))
                {
                    DialogParameters param = this.ModelDialogParameters("Confirm", str);
                    param.Closed = DoConfirmClosed;
                    EFWindow.Confirm(param);
                }
                else if("no".Equals(mark))
                {
                    this.OpenEFWindowAlert(str, true);
                }
            }
            else
            {
                //操作成功关闭新增页面
                //此处可以扩展是否连续新增
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 确认框，点击确认按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);
                return;
            }
            if (e.DialogResult.Value)
            {
                string method = "insertAgain";
                if(Mode.Equals(OperationMode.Update))
                {
                    method = "updateAgain";
                }
                base.Submit(this.BuildSubmitEIInfo("ZMCE01", method, "result"), DoCommitCompletedAgain);
            }
        }

        /// <summary>
        /// 确认框点击确认以后的回调方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DoCommitCompletedAgain(object sender, DoWorkCompletedEventArgs e)
        {
            EFWindow window = this.Parent as EFWindow;
            window.DialogResult = true;
            window.Close();
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 带出设备编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdEquipmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null && e.OldValue.Equals(""))
            {
                return;
            }

            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;

            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdEquipmentCode.Value = picker.ItemObject.GetType().GetProperty("fdCode").GetValue(picker.ItemObject, null) as string;
                    this.fdUseDepartment.Value = picker.ItemObject.GetType().GetProperty("fdDepartment").GetValue(picker.ItemObject, null) as string;
                    this.fdUseDepartmentId.Value = picker.ItemObject.GetType().GetProperty("fdDepartmentId").GetValue(picker.ItemObject, null) as string;
                }
                //没选中时赋予空字符串
                else
                {
                    this.fdEquipmentCode.Value = "";
                    this.fdUseDepartment.Value = "";
                    this.fdUseDepartmentId.Value = "";
                }
            }
        }

        /// <summary>
        /// 判定EFDataField的值是否为空
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private bool NotNullOrEmpty(EFDataField field)
        {
            bool flag = false;
            if ((field.Value != null) && (!field.Value.ToString().Trim().Equals("")))
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 执行部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null);
            }
        }
    }
}
